package practicedemo81;

import java.io.*;
import java.net.*;
import java.util.regex.*;

public class Test2 {
    public static void main(String[] args) {
        try {
            // 创建URL对象
            URL url = new URL("https://www.pv138.com/idCard/list/");

            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

            // 检查HTTP响应码，确保请求成功
            int responseCode = connection.getResponseCode();
            if (responseCode != HttpURLConnection.HTTP_OK) {
                throw new RuntimeException("Failed : HTTP error code : " + responseCode);
            }

            // 使用BufferedReader读取网页内容
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder content = new StringBuilder();

            // 读取网页内容
            while ((line = br.readLine()) != null) {
                content.append(line);
            }
            br.close();
            connection.disconnect();

            // 身份证号码正则表达式（更完整的模式，匹配18位身份证）
            Pattern pattern = Pattern.compile("(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9X])");

            // 使用Pattern和Matcher从内容中查找所有匹配的身份证号码
            Matcher matcher = pattern.matcher(content.toString());

            // 输出匹配到的身份证号码
            while (matcher.find()) {
                System.out.println(matcher.group());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

